define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'withdraw_auto/index',
                    add_url: '',
                    edit_url: 'withdraw_auto/edit',
                    del_url: '',
                    multi_url: '',
                    table: 'withdraw_auto',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'mark', title: __('Mark')},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'),searchList: {"1": "完成", "0": "未处理"},formatter: Controller.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            var checkboxes = document.getElementsByName('data');
            $("#checkbox").click(function() {
                for (var i = 0; i < checkboxes.length; i++) {
                    var checkbox = checkboxes[i];
                    if (!$(this).get(0).checked) {
                        checkbox.checked = false;
                    } else {
                        checkbox.checked = true;
                    }
                }
            });
            $(document).on("click", ".btn-exec", function () {
                var status = $(this).data('status');
                var id = $(this).data('id');
                if (status == "2") {
                    layer.prompt({
                        title: '输入失败原因',
                    },function(value, index, elem){
                        layer.close(index);
                        Fast.api.ajax({
                            url: "withdraw/status?status=2&ids="+id+"&back="+value,
                        }, function (data, ret) {
                            var html = "<font color='red'><i class='fa fa-remove'></i></font>";
                            $(".status"+id).html(html);
                            return false;
                        });
                    });
                }else{
                    Fast.api.ajax({
                        url: "withdraw/status?ids="+id+"&status="+status,
                    }, function (data, ret) {
                        if (status == "1") {
                            var html = "<font color='green'><i class='fa fa-check'></i></font>";
                        }else if(status == "0"){
                            var html = "<font color='blue'><i class='fa fa-refresh'></i></font>";
                        }else{
                            var html = "<font color='red'><i class='fa fa-remove'></i></font>";
                        }
                        $(".status"+id).html(html);
                    });
                }
            });

            $(document).on("click", ".btn-transfer", function () {
                var ids='';
                var arrChk=$("input[name='data']:checked"); 
                if(arrChk.length<=0){
                    layer.msg('请先勾选数据');
                    return false;
                }
                $(arrChk).each(function(){ 
                    ids+=this.id+',';//组装数组
                });
                Fast.api.ajax({
                    url: "withdraw_auto/transfer?ids="+ids,
                }, function (data, ret) {
                    var data = ret.data;
                    for (var i = data.length - 1; i >= 0; i--) {
                        var transferstatus = "<font color='"+(data[i].status == 2 ? 'red' : 'green')+"'>"+data[i].msg+"</font>";
                        $(".transfer"+data[i].id).html(transferstatus);
                        if (data[i].status == 1) {
                            $(".status"+data[i].id).html("<font color='green'><i class='fa fa-check'></i></font>");
                        }
                    }
                });
            });
        },
        editt: function () {
            Controller.api.bindevent();
            var checkboxes = document.getElementsByName('data');
            $("#checkbox").click(function() {
                for (var i = 0; i < checkboxes.length; i++) {
                    var checkbox = checkboxes[i];
                    if (!$(this).get(0).checked) {
                        checkbox.checked = false;
                    } else {
                        checkbox.checked = true;
                    }
                }
            });
            $(document).on("click", ".btn-exec", function () {
                var status = $(this).data('status');
                var id = $(this).data('id');
                if (status == "2") {
                    layer.prompt({
                        title: '输入失败原因',
                    },function(value, index, elem){
                        layer.close(index);
                        Fast.api.ajax({
                            url: "withdraw/status?status=2&ids="+id+"&back="+value,
                        }, function (data, ret) {
                            var html = "<font color='red'><i class='fa fa-remove'></i></font>";
                            $(".status"+id).html(html);
                            return false;
                        });
                    });
                }else{
                    Fast.api.ajax({
                        url: "withdraw/status?ids="+id+"&status="+status,
                    }, function (data, ret) {
                        if (status == "1") {
                            var html = "<font color='green'><i class='fa fa-check'></i></font>";
                        }else if(status == "0"){
                            var html = "<font color='blue'><i class='fa fa-refresh'></i></font>";
                        }else{
                            var html = "<font color='red'><i class='fa fa-remove'></i></font>";
                        }
                        $(".status"+id).html(html);
                    });
                }
            });

            $(document).on("click", ".btn-transfer", function () {
                var ids='';
                var arrChk=$("input[name='data']:checked"); 
                if(arrChk.length<=0){
                    layer.msg('请先勾选数据');
                    return false;
                }
                $(arrChk).each(function(){ 
                    ids+=this.id+',';//组装数组
                });
                Fast.api.ajax({
                    url: "withdraw_auto/transfer?ids="+ids,
                }, function (data, ret) {
                    var data = ret.data;
                    for (var i = data.length - 1; i >= 0; i--) {
                        var transferstatus = "<font color='"+(data[i].status == 2 ? 'red' : 'green')+"'>"+data[i].msg+"</font>";
                        $(".transfer"+data[i].id).html(transferstatus);
                        if (data[i].status == 1) {
                            $(".status"+data[i].id).html("<font color='green'><i class='fa fa-check'></i></font>");
                        }
                    }
                });
            });
        },
        api: {
            formatter: {
                status: function (value, row, index) {
                    if (value == 0) {
                        var i = '<span class="text-danger"><i class="fa fa-circle"></i> 待处理</span>';
                    }
                    if (value == 1) {
                        var i = '<span class="text-success"><i class="fa fa-circle"></i> 已完成</span>';
                    }
                    return i;
                },
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});